package org.opsli.modulars.business.iot.protocol;

import java.util.HashMap;
import java.util.Map;

public class StringToAscii {

    private static String toHexUtil(int n){
        String rt="";
        switch(n){
            case 10:rt+="A";break;
            case 11:rt+="B";break;
            case 12:rt+="C";break;
            case 13:rt+="D";break;
            case 14:rt+="E";break;
            case 15:rt+="F";break;
            default:
                rt+=n;
        }
        return rt;
    }

    public static String toHex(int n){
        StringBuilder sb=new StringBuilder();
        if(n/16==0){
            return toHexUtil(n);
        }else{
            String t=toHex(n/16);
            int nn=n%16;
            sb.append(t).append(toHexUtil(nn));
        }
        return sb.toString();
    }

    public static String parseAscii(String str){
        StringBuilder sb=new StringBuilder();
        byte[] bs=str.getBytes();
        for(int i=0;i<bs.length;i++)
            sb.append(toHex(bs[i]));
        return sb.toString();
    }

    public static String hex2Str(String hex) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < hex.length() - 1; i += 2) {
            String h = hex.substring(i, (i + 2));
            int decimal = Integer.parseInt(h, 16);
            sb.append((char) decimal);
        }
        return sb.toString().replaceAll("[\u0000]", "");
    }


    public static void main(String args[]){
       /* String s="103.046.128.021";
        System.out.println("转换后的字符串是："+StringToAscii.parseAscii(s));*/
       System.out.println(hex2Str("3133392E3030392E3032362E303930000000000000000000000000000000"));
     /*  Map map=new HashMap<>();
       map.put("s",hex2Str("3130332E3034362E3132382E3032310000000000"));
       System.out.println(map);*/


    }

}
